<?php

/**
 * Description of common
 *
 * @author zhangyazhong By 2013-5-18 13:06:38
 * 
 * Info：
 */
class framework_static_common {

    /**
     * 判断字符串是否为空
     * @param type $str
     * @return boolean
     */
    public static function str_empty($str = '') {
        if (!isset($str) || (empty($str)&&$str <> 0) || preg_replace('/\s(?=\s)/', '', trim($str)) == '') {
            return TRUE;
        }
        return FALSE;
    }
    
    
    /**
     * 显示分页
     * @param type $pageSize 每页大小
     * @param type $total 数据总数
     * @param type $currentPage 当前页码
     * @return string
     */
    public static function page($pageSize = 10, $total = 0, $currentPage = null) {
        $url = self::setUrl();
        $pages = ceil($total / $pageSize);
        
        if ($currentPage > $pages) {
            $currentPage = $pages;
        }
        if ($currentPage < 1) {
            $currentPage = 1;
        }
        $pre = $currentPage - 1;
        if ($pre < 1) {
            $pre = 1;
        }
        $next = $currentPage + 1;
        if ($next > $pages) {
            $next = $pages;
        }
        $show = "<ul>";
        if ($currentPage == 1) {
            $show .= "<li><a class='pageBtn back disable' href='{$url}&page={$pre}'>上一页</a></li>";
        } else {
            $show .= "<li><a class='pageBtn back' href='{$url}&page={$pre}'>上一页</a></li>";
        }
        for ($i = 1; $i <= $pages; $i++) {
            if ($i > $currentPage + 2 || $i < $currentPage - 2)
                continue;
            if ($i == $currentPage)
                $show .= "<li><a class='pagenum' href='{$url}&page={$i}'>{$i}</a></li>";
            else
                $show .= "<li><a class='pagenum nowPage' href='{$url}&page={$i}'>{$i}</a></li>";
        }
        
        if ($currentPage == $pages) {
            $show .= "<li><a class='pageBtn back disable' href='{$url}&page={$next}'>下一页</a></li>";
        } else {
            $show .= "<li><a class='pageBtn back' href='{$url}&page={$next}'>下一页</a></li>";
        }
        $show .= "</ul>";
        return $show;
    }
    
    
    public static function setUrl() {
        $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], '?') ? '' : "?");
        $parse = parse_url($url);
        if (isset($parse['query'])) {
            parse_str($parse['query'], $params);
            unset($params['page']);
            $url = $parse['path'] . '?' . http_build_query($params);
        }
        return $url;
    }

}